package com.aisino.bean;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import com.aisino.utils.IdGeneratorUUID;
import com.hxhy.bean.BElectronic;

public class Electronic {

	private String ddbh;
	private String is_apply = "2";
	private String sgbz;
	private String dkbz;
	private String zkbz;
	private String lst;
	private List<ElectronicDetail> kpxm;
	private static String xhf_nsrsbh;
	private static String xhfmc;
	private static String xhf_dz;
	private static String xhf_dh;
	private static String xhf_yhzh;
	private static BigDecimal max_money;
	private static BigDecimal min_money;
	private String ghfmc;
	private String ghf_nsrsbh;
	private String ghf_dz;
	private String ghf_sf;
	private String ghf_gddh;
	private String ghf_sj;
	private String ghf_email;
	private String ghfqylx;
	private String ghf_yhzh;
	private static String kpy;
	private static String sky;
	private static String fhr;
	private String kplx = "1";
	private BigDecimal kphjje;
	private BigDecimal hjbhsje;
	private BigDecimal hjse;
	private BigDecimal ce;
	private String bz;
	private String byzd1;
	private String byzd2;
	private String byzd3;
	private String byzd4;
	private String byzd5;
	private String chyy;

	private String xmlString;

	public Electronic() {
		ddbh = IdGeneratorUUID.getUUID();
		kphjje = BigDecimal.ZERO.setScale(2, BigDecimal.ROUND_HALF_UP);
		hjbhsje = BigDecimal.ZERO.setScale(2, BigDecimal.ROUND_HALF_UP);
		hjse = BigDecimal.ZERO.setScale(2, BigDecimal.ROUND_HALF_UP);
		ce = BigDecimal.ZERO.setScale(2, BigDecimal.ROUND_HALF_UP);
	}

	public Electronic(String xhf_nsrsbh, String xhfmc, String xhf_dz, String xhf_dh, String xhf_yhzh, String kpy,
			String sky, String fhr) {
		ddbh = IdGeneratorUUID.getUUID();
		kphjje = BigDecimal.ZERO.setScale(2, BigDecimal.ROUND_HALF_UP);
		hjbhsje = BigDecimal.ZERO.setScale(2, BigDecimal.ROUND_HALF_UP);
		hjse = BigDecimal.ZERO.setScale(2, BigDecimal.ROUND_HALF_UP);
		ce = BigDecimal.ZERO.setScale(2, BigDecimal.ROUND_HALF_UP);
		Electronic.xhf_nsrsbh = xhf_nsrsbh;
		Electronic.xhfmc = xhfmc;
		Electronic.xhf_dz = xhf_dz;
		Electronic.xhf_dh = xhf_dh;
		Electronic.xhf_yhzh = xhf_yhzh;
		Electronic.kpy = kpy;
		Electronic.sky = sky;
		Electronic.fhr = fhr;
	}

	public String getDdbh() {
		return ddbh;
	}

	public void setDdbh(String ddbh) {
		this.ddbh = ddbh;
	}

	public String getIs_apply() {
		return is_apply;
	}

	public void setIs_apply(String is_apply) {
		this.is_apply = is_apply;
	}

	public String getSgbz() {
		return sgbz;
	}

	public void setSgbz(String sgbz) {
		this.sgbz = sgbz;
	}

	public String getDkbz() {
		return dkbz;
	}

	public void setDkbz(String dkbz) {
		this.dkbz = dkbz;
	}

	public String getZkbz() {
		return zkbz;
	}

	public void setZkbz(String zkbz) {
		this.zkbz = zkbz;
	}

	public List<ElectronicDetail> getKpxm() {
		return kpxm;
	}

	public void setKpxm(List<ElectronicDetail> kpxm) {
		this.kpxm = kpxm;
	}

	public String getXhf_nsrsbh() {
		return xhf_nsrsbh;
	}

	public String getXhfmc() {
		return xhfmc;
	}

	public String getXhf_dz() {
		return xhf_dz;
	}

	public String getXhf_dh() {
		return xhf_dh;
	}

	public String getXhf_yhzh() {
		return xhf_yhzh;
	}

	public String getGhfmc() {
		return ghfmc;
	}

	public void setGhfmc(String ghfmc) {
		this.ghfmc = ghfmc;
	}

	public String getGhf_nsrsbh() {
		return ghf_nsrsbh;
	}

	public void setGhf_nsrsbh(String ghf_nsrsbh) {
		this.ghf_nsrsbh = ghf_nsrsbh;
	}

	public String getGhf_dz() {
		return ghf_dz;
	}

	public void setGhf_dz(String ghf_dz) {
		this.ghf_dz = ghf_dz;
	}

	public String getGhf_sf() {
		return ghf_sf;
	}

	public void setGhf_sf(String ghf_sf) {
		this.ghf_sf = ghf_sf;
	}

	public String getGhf_gddh() {
		return ghf_gddh;
	}

	public void setGhf_gddh(String ghf_gddh) {
		this.ghf_gddh = ghf_gddh;
	}

	public String getGhf_sj() {
		return ghf_sj;
	}

	public void setGhf_sj(String ghf_sj) {
		this.ghf_sj = ghf_sj;
	}

	public String getGhf_email() {
		return ghf_email;
	}

	public void setGhf_email(String ghf_email) {
		this.ghf_email = ghf_email;
	}

	public String getGhfqylx() {
		return ghfqylx;
	}

	public void setGhfqylx(String ghfqylx) {
		this.ghfqylx = ghfqylx;
	}

	public String getGhf_yhzh() {
		return ghf_yhzh;
	}

	public void setGhf_yhzh(String ghf_yhzh) {
		this.ghf_yhzh = ghf_yhzh;
	}

	public String getKpy() {
		return kpy;
	}

	public String getSky() {
		return sky;
	}

	public String getFhr() {
		return fhr;
	}

	public String getKplx() {
		return kplx;
	}

	public void setKplx(String kplx) {
		this.kplx = kplx;
	}

	public BigDecimal getKphjje() {
		return kphjje;
	}

	public void setKphjje(BigDecimal kphjje) {
		this.kphjje = kphjje;
	}

	public BigDecimal getHjbhsje() {
		return hjbhsje;
	}

	public void setHjbhsje(BigDecimal hjbhsje) {
		this.hjbhsje = hjbhsje;
	}

	public BigDecimal getHjse() {
		return hjse;
	}

	public void setHjse(BigDecimal hjse) {
		this.hjse = hjse;
	}

	public String getBz() {
		return bz;
	}

	public void setBz(String bz) {
		this.bz = bz;
	}

	public String getByzd1() {
		return byzd1;
	}

	public void setByzd1(String byzd1) {
		this.byzd1 = byzd1;
	}

	public String getByzd2() {
		return byzd2;
	}

	public void setByzd2(String byzd2) {
		this.byzd2 = byzd2;
	}

	public String getByzd3() {
		return byzd3;
	}

	public void setByzd3(String byzd3) {
		this.byzd3 = byzd3;
	}

	public String getByzd4() {
		return byzd4;
	}

	public void setByzd4(String byzd4) {
		this.byzd4 = byzd4;
	}

	public String getByzd5() {
		return byzd5;
	}

	public void setByzd5(String byzd5) {
		this.byzd5 = byzd5;
	}

	public String getLst() {
		return lst;
	}

	public void setLst(String lst) {
		this.lst = lst;
	}

	public String getChyy() {
		return chyy;
	}

	public void setChyy(String chyy) {
		this.chyy = chyy;
	}

	public boolean kpInvoice() {
		return kpInvoice(false);
	}

	public boolean kpInvoice(boolean isHP) {
		StringBuilder sb = new StringBuilder();
		sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

		if (isHP) {
			sb.append("<REQUEST_FPCHXX> ");
			sb.append("<FPKJXX_FPTXX>");
			sb.append("<DDBH>").append(ddbh).append("</DDBH> ");
			sb.append("<IS_APPLY>4</IS_APPLY> ");
			sb.append("<CHYY>").append(chyy).append("</CHYY>");
			sb.append("</FPKJXX_FPTXX>");
			sb.append("</REQUEST_FPCHXX>");
		} else {
			if (empty(this, is_apply, kpxm, xhf_nsrsbh, xhfmc, xhf_dz, xhf_dh, ghfmc, ghfqylx, kpy, kplx)) {
				return false;
			}

			sb.append("<REQUEST_FPKJXX> ");
			sb.append(" <FPKJXX_FPTXX> ");
			sb.append(" <DDBH>").append(ddbh).append("</DDBH> ");
			sb.append(" <IS_APPLY>").append(is_apply).append("</IS_APPLY> ");
			sb.append(" <KPXM>").append(kpxm.get(0).getXmmc()).append("</KPXM> ");
			sb.append(" <SGBZ>").append(nvl(sgbz)).append("</SGBZ> ");
			sb.append(" <DKBZ>").append(nvl(dkbz)).append("</DKBZ> ");
			sb.append(" <ZKBZ>").append(nvl(zkbz)).append("</ZKBZ> ");
			sb.append(" <XHF_NSRSBH>").append(xhf_nsrsbh).append("</XHF_NSRSBH> ");
			sb.append(" <XHFMC>").append(xhfmc).append("</XHFMC> ");
			sb.append(" <XHF_DZ>").append(xhf_dz).append("</XHF_DZ> ");
			sb.append(" <XHF_DH>").append(nvl(xhf_dh)).append("</XHF_DH> ");
			sb.append(" <XHF_YHZH>").append(nvl(xhf_yhzh)).append("</XHF_YHZH> ");
			sb.append(" <GHFMC>");
			sb.append(ghfmc);
			sb.append("</GHFMC> ");
			sb.append(" <GHF_NSRSBH>").append(nvl(ghf_nsrsbh)).append("</GHF_NSRSBH> ");
			sb.append(" <GHF_SF>").append(nvl(ghf_sf)).append("</GHF_SF> ");
			sb.append(" <GHF_DZ>").append(nvl(ghf_dz)).append("</GHF_DZ> ");
			sb.append(" <GHF_GDDH>").append(nvl(ghf_gddh)).append("</GHF_GDDH> ");
			sb.append(" <GHF_SJ>").append(nvl(ghf_sj)).append("</GHF_SJ>");
			sb.append(" <GHF_EMAIL>").append(nvl(ghf_email)).append("</GHF_EMAIL> ");
			sb.append(" <GHFQYLX>").append(nvl(ghfqylx)).append("</GHFQYLX> ");
			sb.append(" <GHF_YHZH>").append(nvl(ghf_yhzh)).append("</GHF_YHZH> ");
			sb.append(" <KPY>").append(nvl(kpy)).append("</KPY> ");
			sb.append(" <SKY>").append(nvl(sky)).append("</SKY> ");
			sb.append(" <FHR>").append(nvl(fhr)).append("</FHR> ");
			sb.append(" <KPLX>").append(nvl(kplx)).append("</KPLX> ");
			sb.append(" <BYZD1>").append("").append("</BYZD1> ");
			sb.append(" <BYZD2>").append("").append("</BYZD2> ");
			sb.append(" <BYZD3>").append("").append("</BYZD3> ");
			sb.append(" <BYZD4>").append("").append("</BYZD4> ");
			sb.append(" <BYZD5>").append("").append("</BYZD5> ");
			for (ElectronicDetail ed : kpxm) {
				sb.append(" <FPKJXX_XMXX> ");
				ed.setXmsl(ed.getXmje().divide(ed.getXmdj(), ed.getXmsl().scale(), BigDecimal.ROUND_HALF_UP));
				sb.append(" <XMMC>").append(ed.getXmmc()).append("</XMMC> ");
				sb.append(" <XMDW>").append(nvl(ed.getXmdw())).append("</XMDW> ");
				sb.append(" <GGXH>").append(nvl(ed.getGgxh())).append("</GGXH> ");
				sb.append(" <XMSL>")
						.append(0 == BigDecimal.ZERO.compareTo(ed.getXmsl()) ? "" : ed.getXmsl().toPlainString())
						.append("</XMSL> ");
				sb.append(" <HSBZ>").append(nvl(ed.getHsbz())).append("</HSBZ> ");
				sb.append(" <XMDJ>")
						.append(0 == BigDecimal.ZERO.compareTo(ed.getXmdj()) ? "" : ed.getXmdj().toPlainString())
						.append("</XMDJ> ");
				sb.append(" <FPHXZ>").append(nvl(ed.getFphxz())).append("</FPHXZ> ");
				sb.append(" <SPBM>").append(nvl(ed.getSpbm())).append("</SPBM> ");
				sb.append(" <ZXBM>").append(nvl(ed.getZxbm())).append("</ZXBM> ");
				sb.append(" <YHZCBS>").append(ed.getYhzcbs()).append("</YHZCBS> ");
				sb.append(" <LSLBS>").append(nvl(ed.getLslbs())).append("</LSLBS> ");
				sb.append(" <ZZSTSGL>").append(nvl(ed.getZzstsgl())).append("</ZZSTSGL> ");
				sb.append(" <KCE>").append("").append("</KCE> ");
				sb.append(" <XMJE>").append(ed.getXmje().toPlainString()).append("</XMJE> ");
				sb.append(" <SL>").append(ed.getSl().toPlainString()).append("</SL>");
				sb.append(" <SE>").append(ed.getSe().toPlainString()).append("</SE>");
				sb.append(" </FPKJXX_XMXX>");
				kphjje = kphjje.add(ed.getXmje()).add(ed.getSe());
				hjbhsje = hjbhsje.add(ed.getXmje());
				hjse = hjse.add(ed.getSe());
				ce = ce.add(ed.getKce());
			}
			sb.append(" <KPHJJE>").append(kphjje.toPlainString()).append("</KPHJJE> ");
			sb.append(" <HJBHSJE>").append(hjbhsje.toPlainString()).append("</HJBHSJE> ");
			sb.append(" <HJSE>").append(hjse.toPlainString()).append("</HJSE> ");
			sb.append(" <BZ>").append(nvl(bz)).append("</BZ> ");
			sb.append(" </FPKJXX_FPTXX>");
			sb.append("</REQUEST_FPKJXX>");

			if (1 == kphjje.compareTo(max_money) || -1 == kphjje.compareTo(min_money)) {
				xmlString = "开票金额超出限额";
				return false;
			}
		}

		xmlString = sb.toString();
		return true;
	}

	public static Electronic hpInvoice(BElectronic ele) {

		if (empty(null, ele.getLsh()))
			return null;

		Electronic electronic = new Electronic();
		electronic.setDdbh(ele.getLsh());
		electronic.setIs_apply("4");
		electronic.setChyy(ele.getRemark());
		electronic.setLst(ele.getAccountids());
		List<ElectronicDetail> lt = new ArrayList<ElectronicDetail>();
		ElectronicDetail ed = new ElectronicDetail();
		ed.setHouseid(ele.getHouseid());
		ed.setAreaid(ele.getTel());
		lt.add(ed);
		electronic.setKpxm(lt);
		electronic.setKphjje(ele.getMoney());
		electronic.setGhf_email(ele.getEmail());
		electronic.setGhf_dz(ele.getDz());
		electronic.setGhf_nsrsbh(ele.getNsrsbh());
		electronic.setGhfmc(ele.getGfmc());

		electronic.kpInvoice(true);
		return electronic;
	}

	public static Electronic spInvoice(Electronic ele) {
		if (empty(null, ele.getDdbh(), ele.getIs_apply()))
			return null;
		ele.xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> " + "<REQUEST_FPCXXX>" + "<FPKJXX_FPTXX>" + "<DDBH>"
				+ ele.ddbh + "</DDBH>" + "<INVOICE_TYPE>" + ("2".equals(ele.is_apply) ? "1" : "2") + "</INVOICE_TYPE>"
				+ "</FPKJXX_FPTXX>" + "</REQUEST_FPCXXX>";
		return ele;
	}

	public String get() {
		return xmlString;
	}

	public String nvl(String str) {
		return str == null ? "" : str;
	}

	private static boolean empty(Electronic ele, Object... obj) {

		for (int i = 0; i < obj.length; i++) {
			if (obj[i] == null) {
				if (ele != null)
					reason(ele);
				return true;
			}
		}

		return false;
	}

	private static void reason(Electronic ele) {
		if (empty(null, ele.kpxm))
			ele.xmlString = "没有账目信息";
		else if (empty(null, xhf_nsrsbh))
			ele.xmlString = "纳税人识别号为空";
		else if (empty(null, xhfmc))
			ele.xmlString = "纳税人名称为空";
		else if (empty(null, xhf_dz))
			ele.xmlString = "纳税人地址为空";
		else if (empty(null, xhf_dh))
			ele.xmlString = "纳税人电话为空";
		else if (empty(null, ele.ghfmc))
			ele.xmlString = "购方名称为空";
		else if (empty(null, ele.ghfqylx))
			ele.xmlString = "购方企业类型为空";
		else if (empty(null, kpy))
			ele.xmlString = "开票人为空";
		else if (empty(null, ele.kplx))
			ele.xmlString = "开票类型为空";
	}

	static {
		Properties p = new Properties();
		try {
			p.load(Electronic.class.getClassLoader().getResourceAsStream("electronic.properties"));
			xhf_nsrsbh = (String) p.get("nsrsbh");
			xhfmc = (String) p.get("nsrmc");
			xhf_dz = (String) p.get("nsrdz");
			xhf_dh = (String) p.get("nsrdh");
			xhf_yhzh = (String) p.get("yhzh");
			kpy = p.getProperty("kpy");
			sky = p.getProperty("sky");
			fhr = p.getProperty("fhr");
			max_money = new BigDecimal(p.getProperty("max"));
			min_money = new BigDecimal(p.getProperty("min"));
		} catch (IOException e) {
		}
	}

}
